package cn.cl.gulimall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.cl.common.utils.PageUtils;
import cn.cl.common.utils.Query;

import cn.cl.gulimall.product.dao.SpuImagesDao;
import cn.cl.gulimall.product.entity.SpuImagesEntity;
import cn.cl.gulimall.product.service.SpuImagesService;


@Service("spuImagesService")
public class SpuImagesServiceImpl extends ServiceImpl<SpuImagesDao, SpuImagesEntity> implements SpuImagesService {

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		IPage<SpuImagesEntity> page = this.page(
				new Query<SpuImagesEntity>().getPage(params),
				new QueryWrapper<SpuImagesEntity>()
		);

		return new PageUtils(page);
	}

	/**
	 * 3、保存spu的图片集 pms_spu_images
	 */
	@Override
	public void saveImages(Long id, List<String> images) {
		if (images != null || images.size() > 0) {
			return;
		} else {
			List<SpuImagesEntity> spuImagesEntities = images.stream().map((img) -> {
				SpuImagesEntity spuImagesEntity = new SpuImagesEntity();
				spuImagesEntity.setSpuId(id);
				spuImagesEntity.setImgUrl(img);
				return spuImagesEntity;
			}).collect(Collectors.toList());
			if (spuImagesEntities != null || spuImagesEntities.size() > 0) {
				this.saveBatch(spuImagesEntities);
			}
		}
	}
}